/*game.js
*/

$(document).ready(function(){
	var game = new Game();
	game.init();

});
function Game(){
  "use strict";
	this.width;
	this.height;
	this.name;
	this.socket;
	this.runningFrame;
	this.frames = new Array();
	this.context;
	var canvas;
	
	this.init = function(){
		this.width = window.innerWidth;
		this.height = window.innerHeight;
		//this.socket = new socket(this, "ws://85.25.193.8:8088/pong");
		this.socket = new socket(this, "ws://localhost:8088/pong");
		canvas = document.getElementById("canvas");
		$('#canvas').attr('height', this.height);
		$('#canvas').attr('width', this.width);
		if(canvas.getContext){
			this.context = canvas.getContext('2d');
			this.context.fillStyle = "#fff";
			this.context.save();
		}else{
			//no canvas support
		}
		//loops einbinden
		this.frames['shell'] = new frameShell(this);
		this.frames['game'] = new framePong(this);
		this.frames['disconnected'] = new frameDisconnected(this);
		this.runningFrame = this.frames['shell'];
		this.runningFrame.start({
			action : 'firststart'
		});
	};
	this.resize = function(x,y){
		$('#canvas').attr('width', x);
		$('#canvas').attr('height', y);
		this.context = canvas.getContext('2d');
		this.context.fillStyle = "#fff";
		this.context.save();
	}
	//methoden um den loop zu wechseln //wird von lopps genutzt
	this.changeRunningFrame = function(name, options){
		this.runningFrame.stop();
		this.runningFrame = this.frames[name];
		this.frames[name].start(options);
	};
	this.onopen = function(msg){
		this.frames['shell'].onopen(msg);
	};
	this.onclose = function(msg){
		this.changeRunningFrame('disconnected', {
			resolution: [],
			possition: [],
		});
	};

	/*call on socket msg*/
	this.update = function(msg){
		var com = msg[0];
		var data = msg[1];
		switch(com){
			case 'setName':
				if(data[0]=='error'){
					rows.push(data[1]);//name wird bereits benutz
					wait = 'name';
				}else{
					this.name = data[0];
					rows.push('your name is '+data[0]);
				}

				break;
			case 'getplayers':
				$(data).each(function(key,value){
					rows.push('            '+value);
				});
				break;
			case 'ask4match':
				rows.push('somebody want to play yes/no');
				wait = 'ask4match';
				waitData = data;
				break;
			case 'matchdeny':
				rows.push('Disconected!!');
				break;
			case 'matchok':
				rows.push('Match Accept');
				this.stopMenu();
				this.loopMode = 'server';
				this.bootStrapGame(data);
				break;
		}
	};






}